#!/bin/bash
#
# update-readmes
#
# Updates a module's README and MAINTAINERS files from the copy in the
# common submodule.

README_FILES="README README.static-linking MAINTAINERS"

if [ ! -f "common/update-readmes" -o ! -f meson_options.txt ]; then
  echo "Run ./common/update-readmes from the top-level source directory of a GStreamer module";
  exit 1;
fi

MAJOR_VERSION=`head -n 5 meson.build  | grep -e '^\s*version\s*:' | sed -e "s%^\s*version\s*:\s*'%%" -e "s%',%%" | sed -e 's/[^0-9]*\([0-9]\)\.\([0-9]*\).*/\1/'`
MINOR_VERSION=`head -n 5 meson.build  | grep -e '^\s*version\s*:' | sed -e "s%^\s*version\s*:\s*'%%" -e "s%',%%" | sed -e 's/[^0-9]*\([0-9]\)\.\([0-9]*\).*/\2/'`

if test x$MAJOR_VERSION = x -o x$MINOR_VERSION = x ; then
  echo "Failed to extract major/minor version";
  exit 1;
fi

let m=$MINOR_VERSION%2
if test $m = 0; then
  SERIES_VERSION="$MAJOR_VERSION.$MINOR_VERSION.x stable series"
else
  SERIES_VERSION="$MAJOR_VERSION.$MINOR_VERSION.x development series"
fi
#echo "$SERIES_VERSION"

for f in $README_FILES ; do
 cp --preserve "common/$f" $f || {
   echo "Failed to update $f"
   exit 1;
 }
done

sed -i "s/@SERIES_VERSION@/$SERIES_VERSION/g" README

if test x$1 = "x--run-git-add"; then
  git add $README_FILES;
fi
